﻿namespace WenAntdUI.Helps
{
    public static class ImageHelp
    {
        /// <summary>
        /// image转base64
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string ToBase64String(this Image image)
        {
            using var ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return Convert.ToBase64String(ms.ToArray());
        }
        /// <summary>
        /// 获取Image
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public static Image GetImage(string base64String)
        {
            byte[] bytes = Convert.FromBase64String(base64String);
            using var ms = new MemoryStream(bytes);
            return new Bitmap(ms);
        }
        /// <summary>
        /// Image转Icon
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static Icon ToIcon(this Image image)
        {
            var bitmap = new Bitmap(image);
            nint hicon = bitmap.GetHicon();
            var icon = Icon.FromHandle(hicon);
            return icon;
        }
    }
}
